﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
   public  class UserRoleService
    {
        /// <summary>
        /// 依据用户id去获取角色id的集合
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static List<int> GetRoleId(int userId)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.RoleUser.Where(p => p.UserId == userId).Select(p=>p.RoleId).ToList();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static int BatchDelete(string ids,int userId)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string sql = $"delete RoleUser where RoleId in ({ids}) and UserId = {userId}";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static int BatchAdd(string ids, int userId)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
               string[] idArr =  ids.Split(',');
                foreach (var item in idArr)
                {
                    RoleUser userRole = new RoleUser() { UserId = userId, RoleId = Convert.ToInt32(item) };
                    db.RoleUser.Add(userRole);
                  
                }
                return db.SaveChanges();
            }
        }
    }
}
